#include "QtProj/QGanttChartHeader.qth"
#include "QtProj/QGanttChartHeader.moh"
#include "QGanttChartHourColumnHeader.hpp"
#include "QGanttChartDayColumnHeader.hpp"
#include "QGanttChartWeekColumnHeader.hpp"
#include "QGanttChartMonthColumnHeader.hpp"
#include "QGanttChartYearColumnHeader.hpp"
#include <QtGui/QPainter>
#include <boost/foreach.hpp>
#include <cassert>

namespace QtProj
{

QGanttChartHeader::QGanttChartHeader( QWidget* pParent)
	: QWidget( pParent)
	, zoomLevel_( zoomHours)
	, columnWidth_( 16)
{
	this->setFixedHeight( 32);
	this->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Fixed);

	columnHeaders_.push_back( new QGanttChartHourColumnHeader);
	columnHeaders_.push_back( new QGanttChartDayColumnHeader);
	columnHeaders_.push_back( new QGanttChartWeekColumnHeader);
	columnHeaders_.push_back( new QGanttChartMonthColumnHeader);
	columnHeaders_.push_back( new QGanttChartYearColumnHeader);

	assert( NULL != dynamic_cast< QGanttChartHourColumnHeader*>( columnHeaders_[ zoomHours]));
	assert( NULL != dynamic_cast< QGanttChartDayColumnHeader*>( columnHeaders_[ zoomDays]));
	assert( NULL != dynamic_cast< QGanttChartWeekColumnHeader*>( columnHeaders_[ zoomWeeks]));
	assert( NULL != dynamic_cast< QGanttChartMonthColumnHeader*>( columnHeaders_[ zoomMonths]));
	assert( NULL != dynamic_cast< QGanttChartYearColumnHeader*>( columnHeaders_[ zoomYears]));
}

// ---------------------------------------------------------------------------------------------------------------------

QGanttChartHeader::~QGanttChartHeader()
{
	BOOST_FOREACH( QGanttChartColumnHeader* pHdr, columnHeaders_)
	{
		delete pHdr;
	}
}

// ---------------------------------------------------------------------------------------------------------------------

void QGanttChartHeader::setZoomLevel( ZoomLevels newZoomLevel)
{
	zoomLevel_ = newZoomLevel;
	// TODO: Refresh
	Q_EMIT zoomLevelChanged( newZoomLevel);
}

// ---------------------------------------------------------------------------------------------------------------------

void QGanttChartHeader::setColumnWidth( unsigned int newWidth)
{
	columnWidth_ = newWidth;
	// TODO: Refresh
	Q_EMIT columnWidthChanged( newWidth);
}

// ---------------------------------------------------------------------------------------------------------------------

QGanttChartHeader::ZoomLevels QGanttChartHeader::zoomLevel() const 
{
	return zoomLevel_;
}

// ---------------------------------------------------------------------------------------------------------------------

unsigned int QGanttChartHeader::columnWidth() const
{
	return columnWidth_;
}

// ---------------------------------------------------------------------------------------------------------------------

void QGanttChartHeader::paintEvent( QPaintEvent *event)
{
	QPainter painter( this);

	QRect bandRect( 0, 0, this->width(), this->height());


	QHeaderPaintContext ctx( painter, bandRect, QDateTime::currentDateTime(), this->zoomLevel(), this->columnWidth());

	unsigned colHeaderIndex = static_cast< int>( this->zoomLevel());
	columnHeaders_[ colHeaderIndex]->paint( ctx);
}

} // namespace QtProj
